package com.have.blog.admin.redis.impl;

import com.have.blog.admin.redis.AdminSystemParamCacheService;
import com.have.blog.base.constant.BaseRedisConstant;
import com.have.blog.config.service.RedisService;
import com.have.blog.model.models.SysParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class AdminSystemParaCacheServiceImpl implements AdminSystemParamCacheService {
    @Autowired
    private RedisService redisService;
    @Value("${redis.admin.database}")
    private String REDIS_KEY_DATABASE;

    @Override
    public SysParams getSystemParamByParamKey(String paramKey) {
        String key = REDIS_KEY_DATABASE + BaseRedisConstant.SEGMENTATION +
                BaseRedisConstant.SYSTEM_PARAMS + BaseRedisConstant.SEGMENTATION +
                paramKey;
        return (SysParams) redisService.get(key);
    }

    @Override
    public void saveSystemParamByParamKey(SysParams data, String paramKey, long expiration, TimeUnit timeUnit) {
        String key = REDIS_KEY_DATABASE + BaseRedisConstant.SEGMENTATION +
                BaseRedisConstant.SYSTEM_PARAMS + BaseRedisConstant.SEGMENTATION +
                paramKey;
        redisService.set(key, data, expiration, timeUnit);
    }

    @Override
    public void delSystemParamByParamKey(String paramKey) {
        String key = REDIS_KEY_DATABASE + BaseRedisConstant.SEGMENTATION +
                BaseRedisConstant.SYSTEM_PARAMS + BaseRedisConstant.SEGMENTATION +
                paramKey;
        redisService.del(key);
    }
}
